//
//  Alert.swift
//  GoCash
//
//  Created by jie on 2024/5/23.
//

import SwiftUI

struct AlertTestView: View {
    
    @State var showAlert: Bool = false
    
    var body: some View {
        NavigationView {
            ZStack {
                Button {
                    showAlert = true
                } label: {
                    Text("Show Alert")
                }
                
                if showAlert {
                    withAnimation {
                        Alert1(content: .constant(WarmAlert()), isShow: $showAlert)
                    }
                    
                }
            }
        }
    }
}

#Preview(body: {
    AlertTestView()
})


struct WarmAlert: View {
    var body: some View {
        ZStack {
            Color(.white).cornerRadius(12).frame(width: .infinity, height: 260)
        }
        
    }
}


struct Alert1<Content>: View where Content: View {
    
    @Binding var content: Content
    @Binding var isShow: Bool
    
    @State var alpha: Double = 0.2
    @State var factor: Double = 0.5
    
    var body: some View {
        GeometryReader(content: { geometry in
            VStack {
                Spacer()
                content
                    .background(.white)
                    .cornerRadius(16)
                    .opacity(alpha)
                    .scaleEffect(factor)
                    .padding(.leading, 20).padding(.trailing, 20)
                    .onAppear(){
                        withAnimation(.linear(duration: 0.35)) {
                            alpha = 1
                            factor = 1
                        }
                    }
                Spacer()
            }.onTapGesture {
                
            }
        })
        .opacity(isShow ? 1 : 0)
        .edgesIgnoringSafeArea(.all)
        .background(.black.opacity(isShow ? 0.52 : 0))
        .onTapGesture {
            withAnimation {
                alpha = 0
                factor = 0.5
                isShow.toggle()
            }
            
        }
    }
}

//struct AlertBaseContent: View {
//    
//    
//    var body: some View {
//        ZStack {
//            Color(.white).cornerRadius(12)
//        }
//        
//    }
//}


/*
 GeometryReader(content: { geometry in
     VStack {
         Spacer()
         ZhanQiAlert(
             amount: .constant(order!.cJs8J82CCNA ?? 0),
             days: .constant(order!.psjao92Ja ?? 0),
             date: .constant(order!.usagyu2awsu82a ?? "")) { type in
                 if type == 0 { showZhanQiAlert = false }
                 if type == 1 {
                     showZhanQiAlert = false
                     isShowChannelList.toggle()
                 }
             }
         Spacer()
     }
 })
 .opacity(showZhanQiAlert ? 1 : 0)
 .background(Color.black.opacity(0.52).edgesIgnoringSafeArea(.all).onTapGesture {
     withAnimation {
         
     }
 })
 */

/*
 struct ZhanQiAlert: View {
     
     @State var alpha: Double = 0.2
     @State var factor: Double = 0.5
     
     @Binding var amount: Int
     @Binding var days: Int
     @Binding var date: String
     
     var alertOperation: (Int)->Void
     
     var body: some View {
         ZStack {
             VStack(alignment: .center, spacing: 0) {
                 Color(.clear).frame(height: 20)
                 Text("Defer repayment").font(.system(size: 16, weight: .black))
                 Color(.clear).frame(height: 20)
                 Text("Pay a fixed fee to defer your repayment date and set aside more funds for your daily work and keep your credit history in good standing.").font(.system(size: 12)).foregroundColor(Color(hex: "#030303"))
                 Color(.clear).frame(height: 20)
                 HStack(spacing: 0) {
                     Text("Defered fee").font(.system(size: 12)).foregroundColor(Color(hex: "#030303"))
                     Spacer()
                     Text(amount.priceFormat()).font(.system(size: 12, weight: .bold)).foregroundColor(Color(hex: "#030303"))
                 }.padding(.bottom, 16)
                 
                 HStack(spacing: 0) {
                     Text("Defered days").font(.system(size: 12)).foregroundColor(Color(hex: "#030303"))
                     Spacer()
                     Text("\(days) days").font(.system(size: 12, weight: .bold)).foregroundColor(Color(hex: "#030303"))
                 }.padding(.bottom, 16)
                 
                 HStack(spacing: 0) {
                     Text("Repayment date after deferment").font(.system(size: 12)).foregroundColor(Color(hex: "#030303"))
                     Spacer()
                     Text(date).font(.system(size: 12, weight: .bold)).foregroundColor(Color(hex: "#030303"))
                 }.padding(.bottom, 20)
              
                 Button(action: {
                     hideWithAnimation(type: 1)
                 }, label: {
                     ZStack {
                         Color(hex: "#6D57E9").frame(height: 44).cornerRadius(22)
                         Text("Confirm to pay").font(.system(size: 14, weight: .bold)).foregroundColor(.white)
                     }
                 })
                 
                 Button(action: {
                     hideWithAnimation(type: 0)
                     
                 }, label: {
                     Text("Full Repayment").font(.system(size: 14, weight: .bold)).foregroundColor(Color(hex: "#6D57E9")).padding(.vertical, 16)
                 })
                 
             }.padding(.horizontal, 16)
         }
         .background(.white)
         .cornerRadius(16)
         .opacity(alpha)
             .scaleEffect(factor)
             .padding(.leading, 20).padding(.trailing, 20)
             .onAppear(){
                 withAnimation(.linear(duration: 0.35)) {
                     alpha = 1
                     factor = 1
                 }
             }
     }
     
     func hideWithAnimation(type: Int) {
         withAnimation(.linear(duration: 0.35)) {
             alpha = 0
             factor = 0
         }
         DispatchQueue.main.asyncAfter(deadline: .now()+0.35) {
             alertOperation(type)
         }
     }
 }

 #Preview {
     ZhanQiAlert(
         amount: .constant(1000),
         days: .constant(7),
         date: .constant("May 05,2022")) { _ in
             
         }
 }

 */
